﻿using lyAdmin.Common;
using lyAdmin.Common.Result; 
using lyAdmin.Domain.IDomain;
using lyAdmin.Entity;
using lyAdmin.Entity.Bean;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http.Filters;

namespace lyAdmin.Models
{ 
    public class AdminBaseController<T>: BaseController
    {
        public IBaseService<T> iBaseService;
        public ILogger _logger;
        public AdminBaseController(IBaseService<T> iBaseService, ILogger logger)
        {
            this.iBaseService = iBaseService;
            this._logger = logger;
        }
        /// <summary>
        /// 界面
        /// </summary>
        /// <returns></returns>
        [AuthonizationFilter]
        [ApiExplorerSettings(IgnoreApi = true)]
        public IActionResult Index()
        { 
            ViewData["basePath"] = $"{this.Request.Scheme}://{this.Request.Host}/{this.Request.PathBase}";
            //ViewData["userInfo"] = GetAdmin();
            return View();
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="queryBean"></param>
        /// <returns></returns>
        [AuthonizationFilter]
        [ApiExplorerSettings(IgnoreApi = true)]
        public JsonResult List(QueryBean queryBean)
        {
            IQueryable<T> lst = iBaseService.GetList(queryBean); 
            return AjaxResultUtil.Success(lst.ToList(), lst.Count());
        }

        /// <summary>
        /// 通过id获取实体
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>实体</returns>
        [Route("{id}")]
        [AuthonizationFilter]
        [ApiExplorerSettings(IgnoreApi = true)]
        public JsonResult Get(int id)
        {
            T entity = iBaseService.Get(id);
            return AjaxResultUtil.Success(entity); 
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>JSON格式返回</returns>
        [AuthonizationFilter]
        [ApiExplorerSettings(IgnoreApi = true)]
        public JsonResult Add(T entity)
        {
            int id = iBaseService.Add(entity);
            if (id > 0)
                return AjaxResultUtil.Success();
            else
                return AjaxResultUtil.Fail();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>JSON格式返回</returns> 
        [AuthonizationFilter]
        [ApiExplorerSettings(IgnoreApi = true)]
        public JsonResult Update(T entity)
        {
            int qty = iBaseService.Update(entity);
            if (qty > 0)
                return AjaxResultUtil.Success();
            else
                return AjaxResultUtil.Fail();
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>JSON格式返回</returns>
        [Route("{id}")]
        [AuthonizationFilter]
        [ApiExplorerSettings(IgnoreApi = true)]
        public JsonResult Delete(string id)
        {
            int qty = 0;
            if (id.IndexOf(",") == -1)
            {
                T entity = iBaseService.Get(int.Parse(id));
                qty += iBaseService.Delete(entity);
            }
            else
            {
                string[] ids = id.Split(",");
                for(int i = 0; i < ids.Length; i++)
                {
                    T entity = iBaseService.Get(int.Parse(ids[i]));
                    qty += iBaseService.Delete(entity);
                }
            }
            if (qty > 0)
                return AjaxResultUtil.Success();
            else
                return AjaxResultUtil.Fail();
        }
        [ApiExplorerSettings(IgnoreApi = true)]
        public Admin GetAdmin()
        { 
            string token = GetCookies("token");
            Admin admin = Redis.Get<Admin>(token);
            return admin;
        }
    }
}
